package com.neusoft.neuiotms.dcam.workflow.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.workflow.dto.BaofeiSLDTO;
import com.neusoft.neuiotms.dcam.workflow.model.Annex;
import com.neusoft.neuiotms.dcam.workflow.model.BaofeiItemSL;
import com.neusoft.neuiotms.dcam.workflow.model.BaofeiSL;
import com.neusoft.neuiotms.dcam.workflow.repository.AnnexRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.BaofeiItemSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.BaofeiSLRepository;

@Component
public class BaofeiSLConverter extends AbstractConverter<BaofeiSL, BaofeiSLDTO> {

    @Autowired
    private BaofeiSLRepository baofeiSLRepository;
    @Autowired
    private BaofeiItemSLRepository baofeiItemSLRepository;
    @Autowired
    private BaofeiItemSLConverter baofeiItemSLConverter;
    @Autowired
    private AnnexRepository annexRepository;
    @Autowired
    private AnnexConverter annexConverter;

    @Override
    public BaofeiSL toModel(final BaofeiSLDTO dto) {
        BaofeiSL model;
        if (dto.isNew()) {
            model = new BaofeiSL();
        } else {
            model = this.baofeiSLRepository.getOne(dto.getId());
        }

        final List<BaofeiItemSL> list = model.getDetails().stream().collect(Collectors.toList());
        list.forEach(k -> {
            model.getDetails().remove(k);
            this.baofeiItemSLRepository.delete(k);
        });

        dto.getDetails().stream().map(k -> {
            final BaofeiItemSL detail = this.baofeiItemSLConverter.toModel(k);
            detail.setMain(model);
            return detail;
        }).filter(s -> null != s).forEach(model.getDetails()::add);

        model.getAnnex().clear();
        dto.getAnnex().stream().map(k -> {
            Annex annex = this.annexConverter.toModel(k);
            annex = this.annexRepository.save(annex);
            return annex;
        }).forEach(model.getAnnex()::add);

        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setTaskId(dto.getTaskId());
        model.setTitle(dto.getTitle());
        model.setRemark(dto.getRemark());
        model.setCreateUser(dto.getCreateUser());
        model.setCreateUserName(dto.getCreateUserName());
        model.setDisposeDate(dto.getDisposeDate());
        model.setReason(dto.getReason());
        model.setCategories(dto.getCategories());

        return model;
    }

    @Override
    public BaofeiSLDTO toDTO(final BaofeiSL model, final boolean forListView) {
        final BaofeiSLDTO dto = new BaofeiSLDTO();
        dto.setId(model.getId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setTaskId(model.getTaskId());
        dto.setTitle(model.getTitle());
        dto.setRemark(model.getRemark());
        dto.setCreateUser(model.getCreateUser());
        dto.setCreateUserName(model.getCreateUserName());
        dto.setDisposeDate(model.getDisposeDate());
        dto.setReason(model.getReason());
        dto.setCategories(model.getCategories());
        model.getDetails().stream().map(l -> this.baofeiItemSLConverter.toDTO(l))
                .forEach(dto.getDetails()::add);
        model.getAnnex().stream().map(k -> this.annexConverter.toDTO(k)).forEach(dto.getAnnex()::add);
        return dto;
    }

}
